/*
 * @Author: yan_hai_dong 891236760@qq.com
 * @Date: 2023-03-13 10:27:31
 * @LastEditors: yan_hai_dong 891236760@qq.com
 * @LastEditTime: 2023-03-14 11:05:48
 * @FilePath: \node\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const express = require('express');
const app = express();
const port = 3000;
const http = require('http');
const server = http.createServer(app);
const tencentcloud = require("tencentcloud-sdk-nodejs");

// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
const secretId = 'xxxxxx'
const secretKey = 'xxxxxxxxxx'
app.use(express.json())
// const bodyParser = require('body-parser')
// app.use(bodyParser.urlencoded({
//     extended: false
// }))
// app.use(bodyParser.json())


// 解决跨域
const cors = require('cors')
app.use(cors())

app.get('/', (req, res) => {
    res.send('Hello World!');
});

app.post('/test/img', async (req, res) => {
    async function testImg(dataBase64,Url) {
        const IaiClient = tencentcloud.iai.v20180301.Client;
        // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
        // 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议采用更安全的方式来使用密钥，请参见：https://cloud.tencent.com/document/product/1278/85305
        const clientConfig = {
            credential: {
                secretId,
                secretKey,
            },
            region: "",
            profile: {
                httpProfile: {
                    endpoint: "iai.tencentcloudapi.com",
                },
            },
        };

        // 实例化要请求产品的client对象,clientProfile是可选的
        const client = new IaiClient(clientConfig);
        const params = {
            "Image": dataBase64,
            NeedFaceAttributes: 1,
            NeedQualityDetection: 1,
            Url
        };
        return new Promise((resolve, reject) => {
            client.DetectFace(params).then(
                (data) => {
                    resolve(data)
                },
                (err) => {
                    console.log("error", err);
                    reject(err)
                }
            );
        })

    }
    testImg(req.body.img,req.body.url).then(data => {
        res.send({
            code: 200,
            data
        });
    }).catch(err => {
        res.send({
            code: 500,
            err
        });
    })
});
server.listen(port, () => {
    console.log(`listening on *:${port}`);
});